package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.BaseContext;
import net.zjitc.common.R;
import net.zjitc.entity.AddressBook;
import net.zjitc.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //新增地址
    /*consignee: "123"
    detail: "123"
    label: "学校"
    phone: "13212345678"
    sex: "1"
    请求 URL: http://localhost:8080/addressBook
    请求方法: POST
    * */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        log.info("保存地址:" + addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("保存成功");
    }

    //查询所有地址

    /**
     * 根据用户id查询相应的地址
     * 请求 URL: http://localhost:8080/addressBook/list
     * 请求方法: GET
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBookLambdaQueryWrapper);
        return R.success(list);
    }

    //查询默认地址，用户下单的时候填充在下单页面上的信息


    //设置默认地址

    /**
     * id : "1647795352320688130"
     * 请求 URL: http://localhost:8080/addressBook/default
     * 请求方法: PUT
     *
     * @param addressBook 地址参数
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址" + addressBook.getId());
        //修改所有的地址为非默认地址，将数据库表中，用户id下面的所有的地址，isDefault全部设置为0
        LambdaUpdateWrapper<AddressBook> addressBookLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        addressBookLambdaUpdateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        addressBookLambdaUpdateWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(addressBookLambdaUpdateWrapper);
        //设置当前地址为默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置默认地址成功");
    }


    //根据id查询地址

    /**
     * return $axios({
     * 'url': `/addressBook/${id}`,
     * 'method': 'get',
     * })
     */
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable("id") Long id) {
        log.info("根据id查询地址" + id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    //修改地址

    /**
     * 请求 URL: http://localhost:8080/addressBook
     * 请求方法: PUT
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        log.info("修改地址"+addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


    //删除地址

    /**
     * 请求 URL: http://localhost:8080/addressBook?ids=1647795352320688130
     * 请求方法: DELETE
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("删除id"+ids);
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }

}
